<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="beforeAddVehicle" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle"
             v-model="dialogVisible"
             :before-close="handleClose"
             style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input v-model='saveVehicleForm.brand' placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input v-model="saveVehicleForm.license" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input v-model="saveVehicleForm.model" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input v-model="saveVehicleForm.code" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <el-option  v-for="item in vehicleTypeArr" :label="item.label" :value="item.value" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option v-for="item in vehicleColorTypeArr" :label="item.label" :value="item.value" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" v-model="saveVehicleForm.buyTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" v-model="saveVehicleForm.regTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input v-model="saveVehicleForm.price" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button @click="saveVehicle" type="primary">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input @keydown.enter="loadVehicle" placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.brand"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input @keydown.enter="loadVehicle" placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button @click="loadVehicle" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editVehicle(scope.row.id)" size="small">编辑</el-button>
          <el-button link type="primary" @click="deleteVehicle(scope.row.id)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');

//车辆搜索的条件 品牌 车牌号
const searchVehicleForm = ref({brand:'',license:''});

//车辆表格数据
const vehicleArr=ref([]);

//加载车辆方法
const loadVehicle = ()=>{
  //console.log(searchVehicleForm)
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+"/v1/vehicle/select?"+data).then((response)=>{
    if (response.data.code==2000){
      vehicleArr.value=response.data.data;
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 字典项
const vehicleTypeArr =ref([]);
const batteryTypeArr =ref([]);
const vehicleColorTypeArr =ref([]);
onMounted(()=>{
  loadVehicle();
  loadDictOption(vehicleTypeArr,"vehicle_type");
  loadDictOption(batteryTypeArr,"battery_type");
  loadDictOption(vehicleColorTypeArr,"vehicle_color");
})

//重置按钮功能
const resetSearch = ()=>{
  searchVehicleForm.value={};
  loadVehicle();
}

//车辆状态显示内容进行转换
const vehicleStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==1){
    cellValue = '空闲';
  }else if(cellValue==2){
    cellValue = '占用';
  }
  return cellValue;
}

//对车辆类型进行转换
const vehicleTypeFormatter=(row,column,cellValue,index)=>{
  if (!vehicleTypeArr.value){
    return cellValue;
  }
  for (const item of vehicleTypeArr.value) {
    if (item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

//对电池类型进行转换
const batteryTypeFormatter=(row,column,cellValue,index)=>{
  if (!batteryTypeArr.value){
    return cellValue;
  }
  for (const item of batteryTypeArr.value) {
    if (item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

//车辆弹框表单的所有数据
const saveVehicleForm= ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:'',

})

const beforeAddVehicle =  ()=>{
  dialogTitle.value='新增车辆';
  dialogVisible.value=true;
  saveVehicleForm.value={};
}
//保存车辆
const saveVehicle=()=>{
  // console.log(saveVehicleForm.value)
  let data =qs.stringify(saveVehicleForm.value);
  axios.post(BASE_URL+"/v1/vehicle/save",data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("保存成功");
      searchVehicleForm.value={};
      dialogVisible.value=false;
      loadVehicle();
    }else{
      ElMessage.error("车辆号与车辆识别码不允许重复！");
    }
  })
}
//编辑车辆
const editVehicle = (id)=>{
  dialogVisible.value=true;
  dialogTitle.value='编辑车辆';
  axios.get(BASE_URL+"/v1/vehicle/select?id="+id).then((response)=>{
    if (response.data.code==2000){
      saveVehicleForm.value=response.data.data[0];
    }else{
      ElMessage.error(response.data.message)
    }
  })
}

//删除用户
const deleteVehicle = (id)=>{
  if (confirm("您确定要删除该车辆信息？")){
    axios.post(BASE_URL+"/v1/vehicle/deleteVehicle/"+id).then((response)=>{
      if (response.data.code==2000){
        ElMessage.success("删除车辆成功!");
        loadVehicle();
      }else{
        ElMessage.error(response.data.message);
      }
    })
  }
}
const handleClose=()=>{
  if (confirm("你确认要关闭本弹框吗?")){
    dialogVisible.value=false;
  }
}

</script>